package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.shop.ShopJson;
import com.drinks.yiniang.entity.user.invoice.InvoiceOrderListJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 发票api
 * Created by han on 2019/3/6 0006.
 */

public class InvoiceApiIO extends BaseApiIO {
    volatile static InvoiceApiIO instance;

    static public InvoiceApiIO getInstance() {
        if (instance == null) {
            synchronized (InvoiceApiIO.class) {
                if (instance == null) {
                    instance = new InvoiceApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 发票须知
     *
     * @param callback
     */
    public void getInvoiceNotes(final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_INVOICE_NOTES, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("发票须知===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取开发票的订单
     *
     * @param callback
     */
    public void getInvoiceOrderList(final APIRequestCallback<InvoiceOrderListJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_INVOICE_ORDER_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取开发票的订单===onSuccess");

                InvoiceOrderListJson mDataJson = JSON.parseObject(res, InvoiceOrderListJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 提交发票申请
     *
     * @param type           类型 0个人 1公司
     * @param name           名称
     * @param email          邮件
     * @param invoiceDuty    税号
     * @param address        公司地址
     * @param mobile         手机号
     * @param bank           开户行
     * @param bankAccount    银行账号
     * @param invoiceContent 发票明细 0明细 1大类
     * @param orderIds       订单 ID字符串 (多个逗号分隔1,2)
     * @param callback
     */
    public void submitInvoice(String type, String name, String invoiceDuty, String email, String address, String mobile,
                              String bank, String bankAccount, String invoiceContent, String orderIds,
                              final APIRequestCallback<BaseJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("type", type);
        paramsMap.put("name", name);
        paramsMap.put("email", email);
        paramsMap.put("invoice_duty", invoiceDuty);
        paramsMap.put("address", address);
        paramsMap.put("mobile", mobile);
        paramsMap.put("deposit_bank", bank);
        paramsMap.put("account", bankAccount);
        paramsMap.put("invoice_content", invoiceContent);
        paramsMap.put("order_ids", orderIds);

        MainApiIO.getInstance().postRequest(HttpUrls.API_INVOICE_SUBMIT, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("提交发票申请===onSuccess");

                BaseJson mDataJson = JSON.parseObject(res, BaseJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
